package com.wolfpire.resource.controller.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wolfpire.knowledge.dao.KnowledgeDao;
import com.wolfpire.lessonplan.dao.LessonPlanDao;
import com.wolfpire.resource.controller.dao.ResourceDao;
import com.wolfpire.subject.dao.SubjectDao;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;
import com.wolfpire.system.dao.UserDao;
import com.wolfpire.system.model.Knowledge;
import com.wolfpire.system.model.LessonPlan;
import com.wolfpire.system.model.Profession;
import com.wolfpire.system.model.Subject;
import com.wolfpire.system.model.User;

@Repository("resourceDao")
public class ResourceDaoImpl extends BaseHibernateDao<LessonPlan, Long> implements ResourceDao {

	public List<LessonPlan> list(LessonPlan lessonPlan) {
		Criteria c = createCriteria(createFilter(lessonPlan));
		return c.list();
	}

	public Page<LessonPlan> findLessonPlans(Page<LessonPlan> page,
			LessonPlan filterLessonPlan) {
		StringBuffer querySqlBuffer = new StringBuffer();
		StringBuffer countSqlBuffer = new StringBuffer();
		List<String> propertyNames = new ArrayList<String>();
		List propertyValues = new ArrayList();
		//querySqlBuffer.append(" SELECT u.* FROM t_sm_user u, t_sm_org o, t_sm_user_org uo ")
		querySqlBuffer.append(" SELECT l.* FROM t_lessonplan l  ")
				.append(" LEFT JOIN t_subject_lessonplan sl ON l.id = sl.lessonplan_id  ")
				.append(" LEFT JOIN t_subject_lessonplan s ON s.id = sl.subject_id ")
				.append(" WHERE 1 =1 ");
				//.append(" WHERE u.id = uo.user_id AND uo.org_id = o.id ");
		if (null != filterLessonPlan.getId()) {
			querySqlBuffer.append(" l.id =:id");
			propertyNames.add("id");
			propertyValues.add(filterLessonPlan.getId());
		}
		if (!StringUtils.isBlank(StringUtils.stripToEmpty(filterLessonPlan.getTitle()))) {
			querySqlBuffer.append(" AND l.title LIKE :title ");
			propertyNames.add("title");
			propertyValues.add("%" + StringUtils.stripToEmpty(filterLessonPlan.getTitle()) + "%");
		}
		
		if (null != filterLessonPlan.getSubjectId()) {
			querySqlBuffer.append(" AND sl.subject_id= :subjectId ");
			propertyNames.add("subjectId");
			propertyValues.add(filterLessonPlan.getSubjectId());
		}
		
		if (null != filterLessonPlan.getKnowledgeId()) {
			querySqlBuffer.append(" AND l.knowledgeId= :knowledgeId ");
			propertyNames.add("knowledgeId");
			propertyValues.add(filterLessonPlan.getKnowledgeId());
		}
		
		countSqlBuffer.append(" SELECT count(1) from (").append(querySqlBuffer).append(") AS TOTAL ");
		int totalCount = this.findSqlInt(countSqlBuffer.toString(), propertyNames, propertyValues);
		page.setTotalCount(totalCount);
		querySqlBuffer.append(" ORDER BY s.id DESC ");
		if (0 < totalCount) {
			List<LessonPlan> result = this.setPageParameter(page, LessonPlan.class, querySqlBuffer.toString(), propertyNames, propertyValues);
			page.setDataList(result);
		}
		
		return page;
	}

	public LessonPlan getByTitle(String title) {
		Criterion[] criterions = new Criterion[]{
				Restrictions.eq("title", title)
		};
		return findUniqueByCriteria(criterions);
	}
	
	private Criterion[] createFilter(LessonPlan lessonPlan) {
		if (null == lessonPlan) {
			return null;
		}
		List<Criterion> criterions = new ArrayList<Criterion>();
		if (null != lessonPlan.getId()) {
			criterions.add(Restrictions.eq("id", lessonPlan.getId()));
		}
		if (!StringUtils.isBlank(lessonPlan.getTitle())) {
			criterions.add(Restrictions.like("title", lessonPlan.getTitle(), MatchMode.ANYWHERE));
		}
		return criterions.toArray(new Criterion[criterions.size()]);
	}

}
